package com.gf.exam.controller;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.gf.base.controller.BasicController;
import com.gf.common.constant.Constants;
import com.gf.common.constant.ViewPaths;
import com.gf.common.util.WebHelper;
import com.gf.event.service.EventService;
import com.gf.exam.model.Cell;
import com.gf.exam.model.Exam;
import com.gf.exam.service.ExamCellService;
import com.gf.user.model.User;

@Controller
@RequestMapping("/ppt")
public class PPTController extends BasicController {

	private static final Log log = LogFactory.getLog(PPTController.class);

	@Autowired
	private ExamCellService examCellService;
	
	@Autowired
	private EventService eventService;

	@RequestMapping(value = "/make", method = RequestMethod.GET)
	public ModelAndView PPTIndex() {
		User user = WebHelper.checkAuthorityOfPublish();

		ModelAndView mvc = new ModelAndView();
		mvc.addObject("user", user);
		mvc.setViewName(ViewPaths.PPT_VIEW);
		return mvc;
	}

	@RequestMapping(value = "/make2", method = RequestMethod.GET)
	public ModelAndView PPTIndex2() {
		User user = WebHelper.checkAuthorityOfPublish();

		ModelAndView mvc = new ModelAndView();
		mvc.addObject("user", user);
		mvc.setViewName(ViewPaths.PPT2_VIEW);
		return mvc;
	}

	/**
	 * 预览
	 * 
	 * @return
	 */
	@RequestMapping(value = "/preview", method = RequestMethod.GET)
	public ModelAndView PPTPreview() {
		User user = WebHelper.checkAuthorityOfPublish();

		ModelAndView mvc = new ModelAndView();
		mvc.addObject("user", user);
		mvc.setViewName(ViewPaths.PPT2_PREVIEW);
		return mvc;
	}

	/**
	 * 答卷
	 * 
	 * @return
	 */
	@RequestMapping(value = "/join", method = RequestMethod.GET)
	public ModelAndView PPTJoin(long id) {
		User user = WebHelper.getSessionUser();

		Exam exam = examCellService.join(id);

		ModelAndView mvc = new ModelAndView();
		mvc.addObject("user", user);
		mvc.addObject("exam", exam);
		mvc.setViewName(ViewPaths.PPT2_JOIN);

		return mvc;
	}

	/**
	 * ajax后台得到整体项目的信息<br>
	 * 因为用户要答题
	 * 
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "/getExamInfo", method = RequestMethod.POST)
	public @ResponseBody
	Exam ExamInfo(long examId) {
		log.info("exam info：examId=" + examId);
		User user = WebHelper.getSessionAttribute(Constants.SESSION_USER);
		if (user == null) {
			return null;
		}

		return examCellService.join(examId);
	}

	/**
	 * 一张PPT
	 * 
	 * @param examId
	 * @param number
	 * @return
	 */
	@RequestMapping(value = "/getone", method = RequestMethod.POST)
	public @ResponseBody
	Cell getCell(long examId, int number) {
		log.info("one ppt info：examId=" + examId + " number=" + number);
		User user = WebHelper.getSessionAttribute(Constants.SESSION_USER);
		if (user == null) {
			return null;
		}
		
		return examCellService.getCell(examId, number);
	}

	/**
	 * 得到该PPT下的所有内容
	 * 
	 * @param examId
	 * @return
	 */
	@RequestMapping(value = "/getPPT", method = RequestMethod.POST)
	public @ResponseBody
	List<Cell> getPPT(long examId) {
		User user = WebHelper.getSessionAttribute(Constants.SESSION_USER);
		if (user == null) {
			return new ArrayList<Cell>();
		}
		
		return examCellService.getAllCells(examId);
	}

	/**
	 * 编辑修改PPT<br>
	 * 拥有者才具有权限
	 * 
	 * @param examId
	 * @return
	 */
	@RequestMapping(value = "/editor", method = RequestMethod.GET)
	public ModelAndView editorRequest(long id) {
		User user = WebHelper.checkAuthorityOfPublish();

		ModelAndView mvc = new ModelAndView();
		mvc.addObject("user", user);
		mvc.setViewName(ViewPaths.PPT2_EDITOR);

		return mvc;
	}

	/**
	 * 删除某个项目
	 * 
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "/drop", method = RequestMethod.POST)
	public @ResponseBody boolean deleteProject(long id) {
		User user = WebHelper.getSessionAttribute(Constants.SESSION_USER);
		if (user == null) {
			return false;
		}

		// 只有拥有者和管理员 超级 才有删除的权利
		Exam exam = examCellService.scan(id,user.getId());
		if (exam == null) {
			return false;
		}
		boolean result = examCellService.delete(id);
		//删除相应的事件
		if(result){
			eventService.deleteProjectEvent(id);
		}
		
		return result;
	}

}
